package com.itlong.cloud.controller.operate;

import com.itlong.cloud.POJO.DTO.property.DeviceMediaInfoGetPageDTO;
import com.itlong.cloud.POJO.DTO.property.DeviceMediaSetEnableDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyIntercomGetByProjectIdDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyPadFaceRecognitionGetForMediaDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.DeviceMediaInfoGetVO;
import com.itlong.cloud.POJO.VO.property.DeviceMediaInfoVO;
import com.itlong.cloud.POJO.VO.property.PropertyAllEquipGetVO;
import com.itlong.cloud.POJO.VO.property.PropertyPadFaceEquipVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.equip.property.IDeviceCloudControllerServiceClient;
import com.itlong.cloud.equip.property.IDeviceMediaInfoServiceClient;
import com.itlong.cloud.equip.property.IPropertyEquipServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <desc>
 *      运营设备管理控制器
 * </desc>
 *
 * @createDate2020/12/25
 **/
@RestController
@RequestMapping("marketer/deviceMedia")
public class OperateDeviceMediaManageController {


    @Autowired
    private IDeviceMediaInfoServiceClient iDeviceMediaInfoServiceClient;

    @Autowired
    private IPropertyEquipServiceClient iPropertyEquipServiceClient;

    /**
     * <desc>
     *      分页获取设备多媒体资源
     * </desc>
     *
     * @param deviceMediaInfoGetPageDTO 查询参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/12/25
     */
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public Object getPage(DeviceMediaInfoGetPageDTO deviceMediaInfoGetPageDTO, HttpServletRequest request){
        MessageVO messageVO;
        try {
            deviceMediaInfoGetPageDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceMediaInfoGetPageDTO.class, request);
            //判断参数合法性
            if (null == deviceMediaInfoGetPageDTO) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isNotBlank(deviceMediaInfoGetPageDTO.getPutStartTime()) && StringUtils.isNotBlank(deviceMediaInfoGetPageDTO.getPutEndTime())) {
                Date putStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deviceMediaInfoGetPageDTO.getPutStartTime());
                Date putEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deviceMediaInfoGetPageDTO.getPutEndTime());
                // 投放开始时间必须小于投放结束时间
                if (!(putStartTime.compareTo(putEndTime) < 0)) {
                    return new MessageVO(PropertyErrorCodeEnum.EQUIP_MEDIA_INFO_PUT_TIME_AVAILABLE_ERR.getErrorCode());
                }
            }
            if (StringUtils.isNotBlank(deviceMediaInfoGetPageDTO.getCreateStartTime()) && StringUtils.isNotBlank(deviceMediaInfoGetPageDTO.getCreateEndTime())) {
                Date createStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deviceMediaInfoGetPageDTO.getCreateStartTime());
                Date createEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deviceMediaInfoGetPageDTO.getCreateEndTime());
                // 创建开始时间必须小于创建结束时间
                if (!(createStartTime.compareTo(createEndTime) < 0)) {
                    return new MessageVO(PropertyErrorCodeEnum.MEDIA_INFO_SEARCH_CREATE_TIME_AVAILABLE_ERR.getErrorCode());
                }
            }
            Page<DeviceMediaInfoVO> page = iDeviceMediaInfoServiceClient.getMediaInfoByPage(deviceMediaInfoGetPageDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_设备广告管理】分页查询多媒体信息失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      运营查询单个设备广告信息失败
     * </desc>
     *
     * @param mediaId 多媒体资源id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/12/25
     */
    @RequestMapping(value = "/getOneMedia", method = RequestMethod.POST)
    public Object getOneMedia(String mediaId){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            mediaId = request.getAttribute("mediaId") == null ? null : request.getAttribute("mediaId").toString();
            //多媒体Id不能为空
            if (StringUtils.isBlank(mediaId)){
                return new MessageVO(PropertyErrorCodeEnum.EQUIP_MEDIAID_NOT_NULL.getErrorCode());
            }
            //查询单个设备多媒体信息
            DeviceMediaInfoGetVO deviceMediaInfoVO = iDeviceMediaInfoServiceClient.getOneMediaInfo(mediaId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),deviceMediaInfoVO);
        } catch (Exception e) {
            throw new DataAccessException("【运营_设备广告管理_多媒体信息】精确查询单个设备广告信息失败", e);
        }
    }

    /**
     * <desc>
     *     分页查询项目下所有的门口机设备和围墙机设备
     * </desc>
     *
     * @param propertyIntercomGetByProjectIdDTO 依靠projectId查询该项目下所有云对讲设备数据传输对象
     * @return MessageVo 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/3/26
     */
    @PostMapping(path = "getIntercomList")
    @ResponseBody
    public Object getAllEquipsByProjectId(PropertyIntercomGetByProjectIdDTO propertyIntercomGetByProjectIdDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyIntercomGetByProjectIdDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyIntercomGetByProjectIdDTO.class, request);
            if (propertyIntercomGetByProjectIdDTO == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            // 需求改动需要返回分页结果
            Page<PropertyAllEquipGetVO> page = iPropertyEquipServiceClient.getAllEquipsByProjectId(propertyIntercomGetByProjectIdDTO);
//            List<PropertyAllEquipGetVO> propertyEquipGetVOList = iPropertyEquipServiceClient.getAllEquipsByProjectId(propertyIntercomGetByProjectIdDTO);
//            Map<String,Object> propertyEquipGetVOMap = new HashMap<>();
//            propertyEquipGetVOMap.put("propertyEquipGetVOList",propertyEquipGetVOList);
//            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),propertyEquipGetVOMap);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_设备广告管理】获取门口机、围墙机设备失败", e);
        }
    }

    /**
     * <desc>
     *      一键启用/禁用
     * </desc>
     *
     * @param deviceMediaSetEnableDTO  一键启用/禁用多媒体信息参数对象
     * @return
     * @Author Zhu.yj
     * @createDate  2020/12/25
     */
    @LoggerInfo(operateDesc = LogDescConstant.DEVICE_MEDIA_SETENABLE_LOG_DESC)
    @RequestMapping(path = "/setEnable", method = RequestMethod.POST)
    @ResponseBody
    public Object updateStatus(DeviceMediaSetEnableDTO deviceMediaSetEnableDTO){
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            deviceMediaSetEnableDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceMediaSetEnableDTO.class, request);
            if (null == deviceMediaSetEnableDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //多媒体Id字符串不能为空
            if (StringUtils.isBlank(deviceMediaSetEnableDTO.getMediaIds())) {
                return new MessageVO(PropertyErrorCodeEnum.EQUIP_MEDIAID_NOT_NULL.getErrorCode());
            }
            //禁用选中的设备多媒体信息
            iDeviceMediaInfoServiceClient.setMediaInfoEnable(deviceMediaSetEnableDTO);
            return new MessageVO(deviceMediaSetEnableDTO.getMediaIds(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_运营管理_设备广告管理】一键启用/禁用/删除多媒体信息失败", e);
        }
    }

    /**
     * <desc>
     *      设备广告审核通过
     * </desc>
     *
     * @param deviceMediaSetEnableDTO  设备多媒体广告审核DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/12/25
     */
    @LoggerInfo
    @RequestMapping(value = "audit", method = RequestMethod.POST)
    public Object audit(DeviceMediaSetEnableDTO deviceMediaSetEnableDTO, HttpServletRequest request){
        try{
            deviceMediaSetEnableDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceMediaSetEnableDTO.class, request);
            if (deviceMediaSetEnableDTO == null || StringUtils.isBlank(deviceMediaSetEnableDTO.getMediaId())
                    || StringUtils.isBlank(deviceMediaSetEnableDTO.getAuditStatus())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iDeviceMediaInfoServiceClient.audit(deviceMediaSetEnableDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_运营管理_广告管理】审核通过失败", e);
        }
    }


    /**
     * <desc>
     *     分页查询项目下或多媒体资源所绑定的所有支持平板人脸识别的设备
     * </desc>
     *
     * @param propertyPadFaceRecognitionGetForMediaDTO 使用projectId或mediaId查询该项目下所有支持人脸识别的云电梯或门禁设备数据传输对象
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/05/10
     */
    @PostMapping(path = "getPadFaceEquipByPage")
    @ResponseBody
    public Object getPadFaceEquipByPage(PropertyPadFaceRecognitionGetForMediaDTO propertyPadFaceRecognitionGetForMediaDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyPadFaceRecognitionGetForMediaDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyPadFaceRecognitionGetForMediaDTO.class, request);
            if (propertyPadFaceRecognitionGetForMediaDTO == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyPadFaceEquipVO> page = iPropertyEquipServiceClient.getPadFaceEquipByPage(propertyPadFaceRecognitionGetForMediaDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_设备广告管理】获取多媒体资源所绑定的所有支持平板人脸识别的设备失败", e);
        }
    }
}
